using UnityEngine;
using UnityEngine.UI;

namespace Super
{
	public class PanelBase : UIBase
	{
		[Button("OnDirtyRefresh")]
		public string btnName = "刷新";
		
		public Canvas canvas;

		public override void OnDirtyRefresh()
		{
			base.OnDirtyRefresh();
			canvas = GetComponent<Canvas>();
			canvas.worldCamera = FWRefManager.Ins.UICamera;
			canvas.planeDistance = ValueSettingSO.Ins.planeDistance;
			Transform parent = transform.parent;
			if (parent != null  && parent.name.StartsWith("UI"))
			{
				canvas.sortingLayerName = parent.name;
			}

			// 屏幕适配
			float bestAspectRatio = ValueSettingSO.Ins.bestScreenWidth / ValueSettingSO.Ins.bestScreenHeight;
			float curAspectRatio = (Screen.width * 1.0f) / Screen.height;
			float match = bestAspectRatio > curAspectRatio ? 0 : 1;
			CanvasScaler canvasScaler = GetComponent<CanvasScaler>();
			canvasScaler.matchWidthOrHeight = match;
		}

		public override void AfterClose()
		{
			base.AfterClose();
			PanelManager.Ins.OnClosePanel(this.name);
		}
	}

}
